var toWorkConId=null;
var isModify=null;
var pageNo=null;
var userPostId =null;
$( document ).on( "pagecreate", "#workCon", function( event ) {
	access_token=getParameterByName("access_token", getLocationUrl(event) );
	thisWorkConType = getParameterByName("workConType", getLocationUrl(event) );
	toWorkConId=getParameterByName("toWorkConId", getLocationUrl(event) );
	isModify=getParameterByName("isModify", getLocationUrl(event) );
	pageNo = getParameterByName("pageNo", getLocationUrl(event) );
	userPostId = getParameterByName("userPostId", getLocationUrl(event) );
	all_e=event;
	if(thisWorkConType == 3){
		workConType = '1';
	}else{
		workConType = thisWorkConType;
	}
	
	var date = new Date();
	var logDate = date.getFullYear()+"/"+(date.getMonth()+1)+"/"+date.getDate();
	
//	alert(logDate);
	$("#logDate").val(logDate);
	//返回列表
	$("#workCon_back").on('click', function () {
		goTo2(rootPath+"/jsp/weixin/workConList.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&pageNo="+pageNo+"&userPostId="+userPostId,false);
	});
		$("#workCon_add").show();
		
		bindBtnEvent();
		
		if(isModify=="true"){
			workCon_loadReviewData();
			$("#dataSubmit_workCon").html("修改日志");
		}
});
/**
 * 绑定按钮事件
 */
function bindBtnEvent(){
	//发表
	$("#dataSubmit_workCon").on("click",function (){
		if(isModify=="true"){//修改提交
			startWorkCon("click");
		}else{
			startWorkCon("click");
		}
	});
}
/**
 * 修改模式下，加载日志信息
 */
function workCon_loadReviewData(){
	showLoading();
	$.ajax({
	  	url: rootPath+"/workCon/getWorkConById.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&toWorkConId="+toWorkConId+"&workConType="+workConType,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				setTimeout(function () { 
					var result=data.data;
//					$("#workConInfo_workTime").text(result.create_time);
					var content=result.workcon1;
					var str=result.log_date;
//					content=content.replace(/\\n/g, "</br>");
					content=content.replace(/<\/br\s*\/?>/g,"\n");
					$("#mytext_workCon").html(content);
					if(isNullOrEmpty(str)) str="";
					if(str!=""){ 
						str=str.substring(0, 10);
						$("#logDate").val(str);
					}
					
					hideLoading();
				}, 800);
				
			}else{
				alert_wx("fail",data.msg);
				hideLoading();
			}
	  	}
	});
}

function startWorkCon(eventName){
	var dataAdv=$("#mytext_workCon").val();
	access_token=getQueryString("access_token");
	var logDate=$("#logDate").val();
	if(logDate==undefined || logDate==null || logDate==""){
		alert_wx("fail","亲，工作时间不能为空哦！");
		return false;
	}else if(dataAdv==undefined || dataAdv==null || dataAdv==""){
		alert_wx("fail","亲，工作日志不能为空哦！");
		return false;
	}else if(dataAdv.length<5){
		alert_wx("fail","亲，工作日志要大于等于5个字才行哦！");
		return false;
	}
	$("#dataSubmit_workCon").off(eventName);
	dataAdv=encodeURIComponent(dataAdv);
	showLoading();
	$.ajax({
	  	url: rootPath+"/workCon/addOrUpdateCon.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&workcon1="+dataAdv+"&toWorkConId="+toWorkConId+"&workConType="+workConType+"&logDate="+logDate,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				$("#mytext_workCon").val("");
//				$("#name_workCon").html("0/140"); 
				goTo2(rootPath+"/jsp/weixin/workConList.jsp?access_token="+access_token+"&workConType="+thisWorkConType+"&pageNo="+pageNo+"&userPostId="+userPostId,false);
			}else{
				alert_wx("fail",data.msg);
				hideLoading();
			}
			//重新绑定按钮事件
			bindBtnEvent();
	  	}
	});
}